import { userGetInfoService } from '@/api/user'
import { defineStore } from 'pinia'
import { ref } from 'vue'
import router from '@/router'
export const useUserStore = defineStore(
  'user',
  () => {
    const token = ref('') //定义token
    const setToken = (t) => (token.value = t) //设置token
    const user = ref({})
    const getUser = async () => {
      const res = await userGetInfoService()
      user.value = res.data.data
    }
    const setUser = (user) => (user.value = user)
    const logout = () => {
      token.value = ''
      user.value = {}
      localStorage.removeItem('bigevent2024-user-token')
      router.push('/login')
    }
    return { token, setToken, user, getUser, logout, setUser }
  },
  {
    // persist:true
    persist: {
      key: 'bigevent2024-user-token',
      paths: ['token']
    }
  }
)
